package com.ugoodtech.mdcc.web.dto;


import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.utils.BeanUtilEx;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Copyright © 2013 All Rights Reserved, Ugood Technology, Inc.
 */

public class UserResponse {
    private Long clientUserId;
    private String username;                    // required
    private String realName;
    private String telephone;
    private String creationTime;
    private String birthdayStr;
    private String sex;
    private String email;
    private String dealerName;
    private Boolean enabled;
    private String enableStr;
    private String role;
    private String roleStr;
    private String remarks;

    public Long getUserId() {
        return clientUserId;
    }

    public void setUserId(Long clientUserId) {
        this.clientUserId = clientUserId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(String creationTime) {
        this.creationTime = creationTime;
    }

    public String getBirthdayStr() {
        return birthdayStr;
    }

    public void setBirthdayStr(String birthdayStr) {
        this.birthdayStr = birthdayStr;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDealerName() {
        return dealerName;
    }

    public void setDealerName(String dealerName) {
        this.dealerName = dealerName;
    }

    public String getEnableStr() {
        return enableStr;
    }

    public void setEnableStr(String enableStr) {
        this.enableStr = enableStr;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getRoleStr() {
        return roleStr;
    }

    public void setRoleStr(String roleStr) {
        this.roleStr = roleStr;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public static UserResponse turnUserToResponse(User clientUser) throws InvocationTargetException, IllegalAccessException {
        if (null != clientUser) {
            SimpleDateFormat defaultFormat = new SimpleDateFormat(Constants.DATE_TIME_FORMAT);
            SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);
            UserResponse clientUserResponse = new UserResponse();
            BeanUtilEx.copyProperties(clientUserResponse, clientUser);
            if (null != clientUser.getCreationTime()) {
                clientUserResponse.setCreationTime(defaultFormat.format(clientUser.getCreationTime()));
            }
            clientUserResponse.setUserId(clientUser.getId());

//            if (null != clientUser.getBirthday()) {
//                clientUserResponse.setBirthdayStr(dateFormat.format(clientUser.getBirthday()));
//            }
            if (clientUser.isEnabled()) {
                clientUserResponse.setEnableStr(Constants.USER_ENABLED);
            } else {
                clientUserResponse.setEnableStr(Constants.USER_DISABLED);
            }
//            for (Role role : clientUser.getRoles()) {
//                clientUserResponse.setRole(role.getName());
//                clientUserResponse.setRoleStr(role.getDescription());
//            }
            return clientUserResponse;
        } else {
            return null;
        }
    }

    public static User turnResponseToUser(UserResponse clientUserResponse) throws ParseException, InvocationTargetException, IllegalAccessException {
        if (null != clientUserResponse) {
            SimpleDateFormat defaultFormat = new SimpleDateFormat(Constants.DATE_TIME_FORMAT);
            User clientUser = new User();
            BeanUtilEx.copyProperties(clientUser, clientUserResponse);
            return clientUser;
        } else {
            return new User();
        }
    }
}
